package com.SouthernWall_404.AgeSuitcase.Client.Gui.Buttons;

import com.SouthernWall_404.AgeSuitcase.Common.API.GuiSoundHelper;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.Util;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.PlainTextButton;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentUtils;
import net.minecraft.network.chat.Style;
import net.minecraft.util.Mth;

public class TransformablePlainTextButton extends PlainTextButton {


    private float scaleX = 1.0f;
    private float scaleY = 1.0f;
    private float rotation = 0.0f;
    private final Component message;
    private final Component underlinedMessage;
    public Font font;
    public int color;
    private boolean hasUnderLine=true;

    public TransformablePlainTextButton(int x, int y, int width, int height, Component message, OnPress onPress, Font font,int renderX,int renderY,int color) {
        super(x, y, width, height, message, onPress, font);
        this.scaleX = renderX/this.width;
        this.scaleY = renderY/this.height;
        this.message = message;
        this.underlinedMessage = ComponentUtils.mergeStyles(message.copy(), Style.EMPTY.withUnderlined(true));
        this.font=font;

        this.color=color;
    }
    public TransformablePlainTextButton(int x, int y, int width, int height, Component message, OnPress onPress, Font font,int renderX,int color) {
        super(x, y, width, height, message, onPress, font);
        this.scaleX = renderX/this.width;
        this.scaleY = (int)renderX*((float)height/width)/this.height ;

        this.message = message;
        this.underlinedMessage = ComponentUtils.mergeStyles(message.copy(), Style.EMPTY.withUnderlined(true));
        this.font=font;

        this.color=color;
    }


    public TransformablePlainTextButton(int x, int y, int width, int height, Component message, OnPress onPress, Font font,float scale,int color) {
        super(x, y, width, height, message, onPress, font);
        this.scaleX =scale;
        this.scaleY =scale;
        this.message = message;
        this.underlinedMessage = ComponentUtils.mergeStyles(message.copy(), Style.EMPTY.withUnderlined(true));
        this.font=font;

        this.color=color;
    }

    public TransformablePlainTextButton(int x, int y, int width, int height, Component message, OnPress onPress, Font font,float scale,int color,boolean hasUnderLine) {
        super(x, y, width, height, message, onPress, font);
        this.scaleX =scale;
        this.scaleY =scale;
        this.message = message;
        this.underlinedMessage = ComponentUtils.mergeStyles(message.copy(), Style.EMPTY.withUnderlined(true));
        this.font=font;

        this.color=color;
        this.hasUnderLine=hasUnderLine;
    }


    @Override
    public void renderWidget(GuiGraphics guiGraphics, int p_282710_, int p_282486_, float p_281727_) {

        Component component=Component.empty();
        if(hasUnderLine)
        {
            component = this.isHoveredOrFocused() ? this.underlinedMessage : this.message;
        }
        else
        {
            component = this.message;
        }

        guiGraphics.drawString(this.font,component,this.getX(), this.getY(),color,false);
//        guiGraphics.drawString(this.font, component, this.getX(), this.getY(), 16777215 | Mth.ceil(this.alpha * 255.0F) << 24);
    }
    @Override
    public void render(GuiGraphics graphics, int mouseX, int mouseY, float partialTicks) {
        if (!this.visible) return;

        PoseStack poseStack=graphics.pose();
        poseStack.pushPose();
        poseStack.scale(this.scaleX,this.scaleY,1f);
        poseStack.translate(this.getX()/this.scaleX-this.getX(),this.getY()/this.scaleY-this.getY(),0);


        // 渲染按钮
        super.render(graphics, 0, 0, partialTicks);

        // 恢复变换状态
        poseStack.popPose();
    }

    @Override
    public boolean isMouseOver(double mouseX, double mouseY) {
        // 简化处理：使用原始矩形区域进行检测
        // 对于更精确的检测，需要实现更复杂的几何变换
        return mouseX >= this.getX() &&
                mouseY >= this.getY() &&
                mouseX < (this.getX() + this.width* scaleX )&&
                mouseY < (this.getY() + this.height* scaleY);
    }

    @Override
    public boolean mouseClicked(double p_93641_, double p_93642_, int p_93643_) {


        if (this.active && this.visible) {
            if (this.isValidClickButton(p_93643_)) {
                boolean flag = this.clicked(p_93641_, p_93642_);
                if (flag) {
                    GuiSoundHelper.playButtonClick();
                    this.onClick(p_93641_, p_93642_);
                    return true;
                }
            }

            return false;
        } else {
            return false;
        }
    }

    @Override
    protected boolean clicked(double mouseX, double mouseY) {
        return this.active && this.visible && mouseX >= (double)this.getX() && mouseY >= (double)this.getY() && mouseX < (double)(this.getX() + this.width* scaleX) && mouseY < (double)(this.getY() + this.height* scaleY);

    }


}
